//
//  CountdownViewController.m
//  MyFramework
//
//  Created by 余洋 on 2018/8/13.
//  Copyright © 2018年 余洋. All rights reserved.
//

#import "CountdownViewController.h"

#import "POP.h"

@interface CountdownViewController ()

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation CountdownViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"10秒挑战";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)countdownAnimation {
    POPAnimatableProperty *prop = [POPAnimatableProperty propertyWithName:@"prop" initializer:^(POPMutableAnimatableProperty *prop) {
        prop.writeBlock = ^(id obj, const CGFloat *values) {
            self.timeLabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d",(int)values[0]/60,(int)values[0]%60,(int)(values[0]*100)%100];
        };
    }];
    POPBasicAnimation *anBasic = [POPBasicAnimation linearAnimation];   //秒表当然必须是线性的时间函数
    anBasic.property = prop;    //自定义属性
    anBasic.fromValue = @(0);   //从0秒开始
    anBasic.toValue = @(60);     //最大为20秒
    anBasic.duration = 60;    //持续1分钟
    anBasic.beginTime = CACurrentMediaTime() + 1.0f;    //延迟1秒开始
    [self.timeLabel pop_addAnimation:anBasic forKey:@"countdown"];
}

- (IBAction)startBtnAction:(id)sender {
    [self countdownAnimation];
}

- (IBAction)stopBtnAction:(id)sender {
    [self.timeLabel pop_removeAnimationForKey:@"countdown"];
    if ([@"00:10:00" isEqualToString:self.timeLabel.text]) {
        self.timeLabel.text = @"啪啪啪！";
    }
}
@end
